<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateIdentificationAPIRequest;
use App\Http\Requests\API\UpdateIdentificationAPIRequest;
use App\Models\Identification;
use App\Repositories\IdentificationRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class IdentificationController
 * @package App\Http\Controllers\API
 */

class IdentificationAPIController extends ApiBaseController
{
    /** @var  IdentificationRepository */
    private $identificationRepository;

    protected $model_name = 'App\Models\Identification';
    protected $model_load_array = [
    ];
    protected $search_field_array = [
        'name','phone','bank_card','identity_card'
    ];

    public function __construct(IdentificationRepository $identificationRepo)
    {
        $this->identificationRepository = $identificationRepo;
    }

    public function auth(Request $request)
    {
        $name = $request->name;
        $phone = $request->phone;
        $bank_card = $request->bank_card;
        $identity_card = $request->identity_card;

        if($bank_card && $identity_card && $name && $phone){
            $has4 = Identification::where([
                ['name',$name],
                ['phone',$phone],
                ['bank_card',$bank_card],
                ['identity_card',$identity_card],
            ])->orderBy('validity','desc')->first();
            if($has4)
                return responseZK(1,$has4);
        }
        if ($name && $phone){
            $has2 = Identification::where([
                ['name',$name],
                ['phone',$phone],
            ])->orderBy('validity','desc')->first();
            if($has2)
                return responseZK(1,$has2);
            else
                return responseZK(0,null,'没有认证过');
        }else{
            return responseZK(0,null,'缺少参数');
        }
    }

    public function saveAll(Request $request)
    {
        $model = $this->getModel();

        $fields = $model->fillable;
        $_array = [];
        foreach ($fields as $field) {
            $fields_array = $request->{$field};
            if(is_string($fields_array))
                $fields_array = json_decode($fields_array,true);

            if(is_array($fields_array)){
                foreach ($fields_array as $k=>$v) {
                    $_array[$k][$field] = $v;
                }
            }
        }

        foreach ($_array as $item) {
            Identification::create($item);
        }

        return responseZK(1,$_array);
    }
}
